﻿Option Explicit On
Option Strict On
Imports System.Data
Imports System.Text
Imports System.Configuration
Imports MySql.Data.MySqlClient

Public Class frmBrowserMember

    Dim Conn As New MySqlConnection()
    Dim com As New MySqlCommand()
    Dim sb As New StringBuilder()
    Dim dr As MySqlDataReader
    Dim dt As DataTable
    Dim tr As MySqlTransaction
    Public id_m As String
    Public na As String

    Private Sub frmBrowserMember_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim strConn As String = ConfigurationManager.ConnectionStrings("Equ").ConnectionString
        If Conn.State = ConnectionState.Open Then
            Conn.Close()
        End If
        Conn.ConnectionString = strConn
        Conn.Open()
        ShowMember()
        FormatMember()
    End Sub

    Private Sub ShowMember()
        sb.Remove(0, sb.Length)
        sb.Append("SELECT * FROM member")
        Dim sql As String = sb.ToString()
        With com
            .CommandText = sql
            .CommandType = CommandType.Text
            .Connection = Conn
            dr = .ExecuteReader()
            If dr.HasRows Then
                dt = New DataTable()
                dt.Load(dr)
                DataGrid.DataSource = dt
            Else
                DataGrid.DataSource = Nothing
            End If
            dr.Close()
        End With
    End Sub

    Private Sub FormatMember()
        If DataGrid.RowCount > 0 Then
            With DataGrid
                .Columns(0).HeaderText = "รหัส"
                .Columns(1).HeaderText = "ชื่อ-สกุล"
                .Columns(2).HeaderText = "สถานะ"
                .Columns(3).HeaderText = "อีเมล์"
                .Columns(0).Width = 100
                .Columns(1).Width = 200
                .Columns(2).Width = 100
                .Columns(3).Width = 150
            End With
        End If
    End Sub

    Public Property ID() As String
        Get
            Return id_m
        End Get
        Set(value As String)
            id_m = value
        End Set
    End Property

    Public Property Name_m() As String
        Get
            Return na
        End Get
        Set(value As String)
            na = value
        End Set
    End Property

    Private Sub DataGrid_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGrid.CellDoubleClick
        If e.RowIndex = -1 Then
            Exit Sub
        End If
        ID = DataGrid.Rows(e.RowIndex).Cells("Member_ID").Value.ToString()
        Name_m = DataGrid.Rows(e.RowIndex).Cells("Name").Value.ToString()
        DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub txtSearch_KeyDown(sender As Object, e As KeyEventArgs) Handles txtSearch.KeyDown
        If txtSearch.Text.Trim() = "" Then
            Exit Sub
        End If
        If e.KeyCode = Keys.Enter Then
            sb.Remove(0, sb.Length)
            sb.Append("SELECT * FROM member ")
            Dim sql As String = sb.ToString()
            With com
                .CommandType = CommandType.Text
                .CommandText = sql
                .Connection = Conn
                dr = .ExecuteReader()
                If dr.HasRows Then
                    dt = New DataTable()
                    dt.Load(dr)
                    DataGrid.DataSource = dt
                Else
                    DataGrid.DataSource = Nothing
                End If
                dr.Close()
            End With
        End If
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        sb.Remove(0, sb.Length)
        sb.Append("SELECT * FROM member ")
        Dim sql As String = sb.ToString()
        With com
            .CommandType = CommandType.Text
            .CommandText = sql
            .Connection = Conn
            dr = .ExecuteReader()
            If dr.HasRows Then
                dt = New DataTable()
                dt.Load(dr)
                DataGrid.DataSource = dt
            Else
                DataGrid.DataSource = Nothing
            End If
            dr.Close()
        End With
    End Sub
End Class